package com.atguigu.controller;


import com.atguigu.entity.BaseBrand;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseBrandService;
import com.atguigu.utils.MinioUploader;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author zhangqiang
 * @since 2022-08-26
 */
@RestController
@RequestMapping("/product/brand")
public class BrandController {
    //product/brand/queryBrandByPage/1/10

    @Autowired
    private BaseBrandService baseBrandService;
    @Autowired
    private MinioUploader minioUploader;

    @GetMapping("/queryBrandByPage/{pageNum}/{pageSize}")
    public RetVal queryBrandByPage(@PathVariable Long pageNum,@PathVariable Long pageSize){
        Page<BaseBrand> page = new Page<>(pageNum,pageSize);
        baseBrandService.page(page,null);
        return RetVal.ok(page);
    }

    //添加品牌
    @PostMapping
    public RetVal save(@RequestBody BaseBrand baseBrand){
        baseBrandService.save(baseBrand);
        return RetVal.ok();
    }

    //根据id查询产品信息数据回显
    @GetMapping("{brandId}")
    public RetVal BrandById(@PathVariable Long brandId){
        BaseBrand id = baseBrandService.getById(brandId);
        return RetVal.ok(id);
    }

    //修改品牌信息
    @PutMapping
    public RetVal update(@RequestBody BaseBrand baseBrand) {
        baseBrandService.updateById(baseBrand);
        return RetVal.ok();
    }

    //删除品牌信息
    @DeleteMapping("/{brandId}")
    public RetVal delete(@PathVariable Long brandId) {
        baseBrandService.removeById(brandId);
        return RetVal.ok();
    }

    //查询品牌
    @GetMapping("/getAllBrand")
    public RetVal getAllBrand(){
        List<BaseBrand> list = baseBrandService.list(null);
        return RetVal.ok(list);
    }
    //图片上传
    @PostMapping("/fileUpload")
    public RetVal fileUpload(MultipartFile file) throws Exception {
        String result = minioUploader.updateFile(file);
        return RetVal.ok(result);
    }

    //根据品牌id查询品牌信息
    @GetMapping("getBrandById/{brandId}")
    public BaseBrand getBrandById(@PathVariable Long brandId){

        return baseBrandService.getById(brandId);
    }
}

